﻿using AutoMapper;
using Inventory.API.Application.Command.ZonemanagementCommand;
using Inventory.Domain;
using Inventory.Errorcode;
using Inventory.Errorcode.ErrorCode;
using Inventory.Infrastructure.Interfaces;
using MediatR;

namespace Inventory.API.Application.CommandHandler.ZonemanagementCommandHandler
{
    public class AddZonemanagementCommandHanlder : IRequestHandler<AddZonemanagementCommand, ApiResult>
    {


        private readonly ILogger<AddZonemanagementCommandHanlder> logger;
        private readonly IZonemanagementRepository zonemanagementRepository;
        private readonly IMapper mapper;

        public AddZonemanagementCommandHanlder(ILogger<AddZonemanagementCommandHanlder> logger, IZonemanagementRepository zonemanagementRepository, IMapper mapper)
        {
            this.logger = logger;  
            this.zonemanagementRepository = zonemanagementRepository;
            this.mapper = mapper;
        }
        public async Task<ApiResult> Handle(AddZonemanagementCommand request, CancellationToken cancellationToken)
        {
            try
            {
                  var zonemanagement = mapper.Map<Zonemanagement>(request);
                 
                var result=await zonemanagementRepository.InsertAsync(zonemanagement);
                if (result > 0)
                {

                    return ApiResult.Success(StatusCodeEnum.Success);
                }
                else
                {
                    return ApiResult.Fail("新增失败",StatusCodeEnum.NotFound);
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message, "新增失败");
                throw;
            }
        }
    }
}
